<?php
 
include_once("./configure/configure.php");						//--> global var
include_once("./admin/tools/smarty/libs/Smarty.class.php");		//--> out template
include_once("utils/FileUtils.php");				//--> file utils

include_once("impl/CategoryImpl.php");
include_once("impl/ContentImpl.php");
include_once("impl/TagImpl.php");

include_once("includeSmarty.php");


$id 	= $_POST['id'];
$page 	= $_POST['page'];
$order 	= $_POST['order'];


if(empty($id)){
	$location = HOME_URL_HTTP;
	
	header("Location: $location");
	exit;
}

$oContImpl = new ContentImpl(DB_TAG_MM);

$cacheId = $id . '_' . $page;

/*----- out html -----*/
$SUB_DIR	= FileUtils::getDirByString($id) . '/';

$smarty->compile_dir  	= COMPILE_DIR . '/cont/' . $SUB_DIR;
$smarty->cache_dir  	= CACHE_DIR . '/cont/' . $SUB_DIR;

if(!$smarty->isCached('content.html', $cacheId, $cacheId)){	
	$oCatImpl 		= new CategoryImpl(DB_TAG_MM);
	$tagImpl		= new TagImpl(DB_TAG_MM);
	
	
	$oContent 	= $oContImpl->getById($id);
	if(!is_object($oContent)){
		$location = HOME_URL_HTTP;
		
		header("Location: $location");
		exit;
	}
	
	$oImages = $oContImpl->getImagePage($oContent->id, $page);
	
//	if(count($oImages) < 20){
		$oAllRelatedImages	= $oContImpl->getRelatedImagePage($oContent->id, $page);
		
		if(count($oAllRelatedImages) < 10){
			$oPopularImages	= $oContImpl->getPopularImages();
			
			$oAllRelatedImages = array_merge($oAllRelatedImages, $oPopularImages);
		}
//	}else
//		$oAllRelatedImages = array();
		
	
	$oRelatedImages = array();
	foreach($oAllRelatedImages as $oRimg){
		$hasSrcImg = false;
		
		foreach($oImages as $oImg){
			if($oRimg->id == $oImg->id){
				$hasSrcImg = true;
			}
		}
		
		if(!$hasSrcImg)
			$oRelatedImages[] = $oRimg;
	}
	

	
	$oCats 	= $oCatImpl->getLeaf();
	$oTags	= $tagImpl->getTags();
	
	$smarty->assign('oContent', $oContent);
	$smarty->assign('oImages', $oImages);
	$smarty->assign('oRelatedImages', $oRelatedImages);
	
	$smarty->assign('oCat', $oCategory);
	$smarty->assign('oCats', $oCats);
	$smarty->assign('oTags', $oTags);
	
	
	$smarty->assign('id', $id);
	$smarty->assign('order', $order);
}


$oContImpl->onClick($id);

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL_HTTP);
$smarty->assign('DEBUG', DEBUG);


$smarty->display('content.html', $cacheId, $cacheId);
?>
